// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

/*
 * @Author: qiulin_JXGM ql1746365080@gmail.com
 * @Date: 2024-10-11 15:36:29
 * @LastEditors: qiulin 1746365080@qq.com
 * @LastEditTime: 2024-10-24 15:05:05
 * @FilePath: \SGFEM\Import\BDFImport\BulkData\Constraint\SPCParser.cpp
 */

#include "Import/BDFImport/BulkData/Constraint/SPCParser.h"

#include <algorithm>
#include <cstdint>
#include <iostream>

#include "Utility/Logging/SGLogger.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Constraint/ISPCService.h"

#include "Import/BDFImport/BDFParserBase.h"

BULKDATA_ENTRY_PARSER_REG (SPC);

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::DataStructure;
/*
// file://./../../Doc/MarkDown/BulkData/SPC/SPC.md>
*/
// 以下需要修改
enum SPC1Index : std::uint8_t
{
    SPC1Index_SID = 1,
    SPC1Index_G1  = 2,  // Grid or scalar point identification number. (Integer > 0)
    SPC1Index_C1  = 3,  // Component number. See Remark  8. (0 < Integer < 6; up to six Unique Integers, 1through 6, may be placed in the
                        // field with no embedded blanks. 0 or 1applies to scalarpoints and 1 through 6 applies to grid points.)
    SPC1Index_D1 = 4,   // Value of enforced motion for components Gi at grid Ci. (Real; Default = 0.0)
    SPC1Index_G2 = 5,
    SPC1Index_C2 = 6,
    SPC1Index_D2 = 7,
};

SG::DataStructure::Common::Status BDF::BULKDATA::ParseSPC (const std::shared_ptr<SG::DBManager::DBServiceFactory>& dbServiceFactory,
                                                           std::vector<std::string>&                               dividedEntryInfo)
{
    // 定义两个 SPCData 类型的临时对象，用于存储解析出的单点约束数据
    SG::DataStructure::FEM::SPCData tempData1;
    SG::DataStructure::FEM::SPCData tempData2;

    // TODO(bug 001) m_dominId的正确填写
    tempData1.m_domainId = tempData2.m_domainId = 1;

    // 标记是否有第二个节点的约束信息
    bool isHaveSecond = false;

    // 初始化当前字段的索引值
    int curIndex = 0;

    // 遍历分割后的词条信息
    for (auto& curStr : dividedEntryInfo)
    {
        // 检查当前字段是否为空白，如果为空则跳过
        bool const isEmpty = is_all_space (curStr);
        if (isEmpty)
        {
            continue;
            curIndex++;  // 增加索引以便继续遍历
        }

        // 根据索引值决定当前字段的含义
        switch (curIndex)
        {
        case SPC1Index_SID:
            // 解析并存储约束集合的识别号
            tempData1.m_sId = convertToI64 (curStr);
            break;
        case SPC1Index_G1:
            // 解析并存储第一个节点的 ID
            tempData1.m_g = convertToI64 (curStr);
            break;
        case SPC1Index_C1:
            // 解析并存储第一个节点的分量编号
            tempData1.m_c = convertToI64 (curStr);
            break;
        case SPC1Index_D1:
            // 解析并存储第一个节点的强制位移值
            tempData1.m_d = convertToReal (curStr);
            break;
        case SPC1Index_G2:
            // 如果存在第二个节点，设置标记并存储节点 ID
            isHaveSecond  = true;
            tempData2.m_g = convertToI64 (curStr);
            break;
        case SPC1Index_C2:
            // 解析并存储第二个节点的分量编号
            tempData2.m_c = convertToI64 (curStr);
            break;
        case SPC1Index_D2:
            // 解析并存储第二个节点的强制位移值
            tempData2.m_d = convertToReal (curStr);
            break;
        default:
            // 默认情况下不做任何处理
            break;
        }
        // 增加当前字段的索引以便处理下一个字段
        curIndex++;
    }

    // 如果存在第二个节点约束，则将其 SID 与第一个节点一致，并将其添加到数据库
    if (isHaveSecond)
    {
        tempData2.m_sId = tempData1.m_sId;
        dbServiceFactory->get<ISPCService> ()->Add (tempData2);
    }

    // 将第一个节点的约束数据添加到数据库
    return dbServiceFactory->get<ISPCService> ()->Add (tempData1);
}